package org.dfzt.modules.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dfzt.common.system.util.JwtUtil;
import org.dfzt.modules.system.entity.SysUserConfig;
import org.dfzt.modules.system.mapper.SysUserConfigMapper;
import org.dfzt.modules.system.service.ISysUserConfigService;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description: 用户登录参数设置表
 * @Author: jeecg-boot
 * @Date:   2020-06-29
 * @Version: V1.0
 */
@Service
public class SysUserConfigServiceImpl extends ServiceImpl<SysUserConfigMapper, SysUserConfig> implements ISysUserConfigService {
    private static final Log log = LogFactory.getLog(SysUserConfigServiceImpl.class);

    @Override
    @PostConstruct
    public void initUserConfig() {
        log.info("---启动初始化用户登入设置信息---");
        SysUserConfig cfg = this.getById(1);
        if(cfg!=null){
            if(cfg.getSessionOutTime()!=null) JwtUtil.EXPIRE_TIME=cfg.getSessionOutTime()*60*1000;
            if(cfg.getLoginMaxTimes()!=null) JwtUtil.MAX_LOCK_TIMES=cfg.getLoginMaxTimes();
            if(cfg.getClearTime()!=null) JwtUtil.CLEAR_TIME=cfg.getClearTime();
            if(cfg.getLockTime()!=null) JwtUtil.LOCK_TIME=cfg.getLockTime();
            if (cfg.getPasswordValidity()!=null) JwtUtil.PASSWORD_VALIDITY= cfg.getPasswordValidity();
            if (cfg.getMaxSleepTime()!=null) JwtUtil.MAX_SLEEP_TIME= cfg.getMaxSleepTime();
            if (cfg.getSetSleepTime()!=null) JwtUtil.SET_SLEEP_TIME= cfg.getSetSleepTime();
        }
        log.info("会话超时时间--->"+JwtUtil.EXPIRE_TIME+"ms");
        log.info("用户输入错误锁定限制次数--->"+JwtUtil.MAX_LOCK_TIMES+"次");
        log.info("用户输入错误信息清除时间--->"+JwtUtil.CLEAR_TIME+"分钟");
        log.info("用户输入错误锁定时间--->"+JwtUtil.LOCK_TIME+"分钟");
        log.info("用户输入密码有效期--->"+JwtUtil.PASSWORD_VALIDITY+"月");
        log.info("用户输入最大休眠时间--->"+JwtUtil.MAX_SLEEP_TIME+"月");
        log.info("用户输入未登录置为休眠时间--->"+JwtUtil.SET_SLEEP_TIME+"月");
    }

    @Override
    public String getColumn() {
        log.info("----开始生成数据验证json----");
        List<Map<String, String>> list = baseMapper.getColumn();
        JSONObject jb=new JSONObject();
        for(Map<String, String> map:list){
            JSONObject temp=new JSONObject();
            String className = lineToHump(map.get("tableName"),true);
            String columnName = lineToHump(map.get("columnName"),false);

            String columnType = lineToHump(map.get("columnType"),false);
            String type="";
            int max=0;
            int min=0;
            String columnComment = lineToHump(map.get("columnComment"),false);
            if(columnType.startsWith("varchar")){
                max = Integer.parseInt(columnType.substring(columnType.indexOf("(") + 1, columnType.indexOf(")")));
                type="string";
            }
            if(columnType.startsWith("datetime")||columnType.startsWith("date")){
                type="date";
            }
            if(columnType.startsWith("text")||columnType.startsWith("longtext")){
                type="string";
                max=2000;
            }
            if(columnType.startsWith("int")||columnType.startsWith("bigint")||columnType.startsWith("tinyint")){
                type="int";
                max=255;
            }
            temp.put("type",type);
            temp.put("max",max);
            temp.put("min",min);
            if(columnType.startsWith("double")||columnType.startsWith("decimal")){
                temp.put("type","double");
                temp.put("max",500.00);
                temp.put("min",0.00);
            }
            temp.put("comment",columnComment);


            jb.put(className+"."+columnName,temp);

        }
        log.info("------"+jb.toJSONString());
        return jb.toJSONString();
    }


    private static final Pattern linePattern = Pattern.compile("_(\\w)");
    /** 下划线转驼峰
     *
     * flag 是否是实体类名  是的话首字母也大写
     * */
    public String lineToHump(String str,boolean flag) {
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        if(flag){
            char[] name = sb.toString().toCharArray();
            name[0]-=32;
            return new String(name);
        }
        return sb.toString();
    }
}
